package cn.com.generaldata.os.service.indicator_evaluation.composition_analysis;

import org.apache.commons.lang3.Validate;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.jsme_indicator_system.util.Constants;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 海洋第二产业总值占比重评估指标计算类.
 */
public class SecondIndustrytoGOPCalculateService extends IndicatorEvaluationCalculateService {

	private String GOPIndicatorName = ProvinceIndicatorConstant.GOP_INDICATOR_NAME;
	private String SecondIndicatorName = ProvinceIndicatorConstant.SECOND_INDICATOR_NAME;

	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		Double result = null;
		if (indicator.getSection() == Constants.INDICATOR_LEVEL_PROVINCE) {
			result = calculateResult(indicatorAccountVO);
		} else if (indicator.getSection() == Constants.INDICATOR_LEVEL_CITY) {
			result = calculateCityResult(indicator, indicatorAccountVO);
		} else {
			// 县指标没确定先保留
		}
		return result;
	}

	/**
	 * 计算市级指标
	 * 
	 * @param indicatorAccountVO
	 * @return
	 */
	private Double calculateCityResult(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		// if (indicatorAccountVO.getAreaId().equals("320900000000000")) {
		// // return calculateOtherCity(indicatorAccountVO);
		// IndicatorAccountVO vo = BeanMapper.map(indicatorAccountVO, IndicatorAccountVO.class);
		// vo.setAreaId(ProvinceIndicatorConstant.OTHER_CITY_AREA_ID);
		// calculateOtherCity(indicator, vo);
		// }
		return calculateResult(indicatorAccountVO);

	}

	// private void calculateOtherCity(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
	// IndicatorAccountVO vo = BeanMapper.map(indicatorAccountVO, IndicatorAccountVO.class);
	// vo.setAreaId(ProvinceIndicatorConstant.PROVICE_AREA_ID);
	// Double result = Double.valueOf("0");
	// Double provinceGOP = getBaseIndicatorVal(vo, vo.getYear());
	// if (provinceGOP.equals(Double.valueOf(0))) {
	// result = Double.valueOf(0);
	// }
	//
	// Double cityProviceCount = Double.valueOf("0");
	// for (String cityId : ProvinceIndicatorConstant.CITYS) {
	// vo.setAreaId(cityId);
	// cityProviceCount += getBaseIndicatorVal(vo, vo.getYear());
	// }
	//
	// Double otherCityGOP = provinceGOP - cityProviceCount;
	// result = otherCityGOP / provinceGOP * 100;
	//
	// result = new BigDecimal(result).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
	// IndicatorEvaluationResult ier = createIndicatorEvaluationResult(indicator, indicatorAccountVO);
	// ier.setIndicatorValue(result);
	// Criteria cri = getIndicatorAccountingDataCri(indicator, indicatorAccountVO);
	//
	// Record record = dao.fetch("tbl_indicator_evaluation_res", cri);
	// if (record != null) {
	// dao.clear("tbl_indicator_evaluation_res", cri);
	// }
	//
	// saveIndicatorEvaluationResult(ier);
	// }

	@SuppressWarnings("null")
	private Double calculateResult(IndicatorAccountVO indicatorAccountVO) {
		Double currentBase = getBaseIndicatorVal(indicatorAccountVO, indicatorAccountVO.getYear());
		Validate.isTrue(currentBase != null);

		Double currentParent = geSecondIndicatorVal(indicatorAccountVO, indicatorAccountVO.getYear());
		Validate.isTrue(currentParent != null);

		return (currentParent / currentBase) * 100;
	}

	private Double geSecondIndicatorVal(IndicatorAccountVO indicatorAccountVO, int year) {
		return getInputOrAccountingIndicatorValue(indicatorAccountVO, year, SecondIndicatorName);
	}

	private Double getBaseIndicatorVal(IndicatorAccountVO indicatorAccountVO, int year) {
		return getInputOrAccountingIndicatorValue(indicatorAccountVO, year, GOPIndicatorName);
	}

}
